<?
// Data.php
// written by: David Fudge
// created on: November 2, 2008
// last modified: November 2, 2008
//
// description:
// this file contains another layer of abstraction for database interactivity.  this class
// allows us to interact with one class for all database actions while controlling 3 other
// classes from this one. basically this class extends the SQLQuery class and makes them easier to use.

// load dependencies here
ed::load("Core.Core");
ed::load("Core.Error");
ed::load("Data.Database");
ed::load("Data.SQLQuery");
ed::load("Data.SQLResult");

class Data extends SQLQuery
{
	private $db;
	
	// constructor
	public function __construct()
	{
		parent::__construct();
	}
	
	// automatic update fields
	public function autoUpdate()
	{
		// auto update field names are defined in our database config file
		// check for the field in our auto update field name
		if( defined("LASTMODIFIED") ) { $this->setValue(LASTMODIFIED, "/now()/"); }
		if( defined("IPADDRESS") ) { $this->setValue(IPADDRESS, getenv("REMOTE_ADDR")); }
	}
	
	// take an OBJECT argument and easily loop through the indices and values to add them to the SQLQuery object
	// and create our query without having to have a bunch of "db->setValue()" and db->setAuthMethod statements
	// in the calling code
	// NOTE:
	// argument sent to these functions must be an OBJECT keyed as follows:
	// <field name> = <field value>
	public function setValues( $objectOrArray ) { $this->addToValues($objectOrArray); }
	public function setValuesFromObject( $objectOrArray ) { $this->addToValues($objectOrArray); }
	public function addToValues( $objectOrArray )
	{
		// make sure we're getting the object vars unless it's already an array
		$vars = (is_object($objectOrArray)) ? get_object_vars($objectOrArray) : $objectOrArray;
		while( list($k,$v) = each($vars) )
		{
			if( is_object($v) || is_array($v) ) { continue; } // not sure why THIS was here... --->> $this->addToValues($v); }
			else { $this->setValue($k,$v); }
		}
	}
	
	// take an OBJECT argument and easily loop through the indicies and values to add them to the SQLQuery object
	// and create the auth method for our query without having to have a bunch of $db->setAuthMethod() calls
	public function setAuthMethodFromObject( $objectOrArray ) { $this->addToAuthMethod($objectOrArray); }
	public function addToAuthMethod( $objectOrArray )
	{
		// make sure we're getting the object vars unless it's already an array
		$vars = (is_object($objectOrArray)) ? get_object_vars($objectOrArray) : $objectOrArray;
		while( list($k,$v) = each($vars) )
		{
			if( gettype($v) == "object" ) { $this->addToAuthMethod($v); }
			else { $this->setAuthMethod($k,$v); }
		}
	}
	
	// database functions
	private function connect() { if( $this->db ) { $this->db->reconnect(); } else { $this->db = new Database(); } }
	public function select() { $this->connect(); return $this->db->select($this); }
	public function delete() { $this->connect(); return $this->db->delete($this); }
	public function insert() { $this->autoUpdate(); $this->connect(); return $this->db->insert($this); }
	public function replace() { $this->autoUpdate(); $this->connect(); return $this->db->replace($this); }
	public function update() { $this->autoUpdate(); $this->connect(); return $this->db->update($this); }
}
?>